﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
using Exhibition.Common.Helper;

namespace Exhibition.RenderTemplete.Controls
{
    public class RenderReportTableColumn
    {

        /// <summary>
        /// 装载报表表头
        /// </summary>
        /// <param name="tablename"></param>
        /// <returns></returns>
        public static List<ReportTableColumn> LoadReportTableColumn(string tablename, string tbConfig)
        {
            // List<string> lst = new List<string>() { "Name", "SourceName", "Visible", "IsData", "IsGroupBy", "IsKey", "IsFixedStyle", "IsMultiSelect" };

            var tables = GetConfigTables(tablename, tbConfig);

            //var cols = from col in tables.Elements()
            //           select new ReportTableColumn(col, "Name", "SourceName", "Visible", "IsData", "IsGroupBy", "IsKey", "IsFixedStyle");
            //return cols.ToList();
            return tables.FirstOrDefault().columns;
        }





        public static List<ReportTable> GetConfigTables(string tbname, string tbConfig)
        {
            try
            {
                XElement config = XElement.Load(HttpContext.Current.Server.MapPath(tbConfig));
                var tables = from ele in config.Descendants("Table")
                             where ele.Attribute("Name").Value == tbname
                             select new ReportTable(ele, "Name", "IsExport", "IsMultiSelect", "IsEdit", "SourceName", "IsAll", "IsOwner");
                return tables.ToList();
            }
            catch (Exception e)
            {
                LogHelper.Error("装载表格错误！" + e.Message);
                throw e;
            }
        }

        public class ReportTable
        {

            public ReportTable(XElement tb, string tbname, string isExport, string isMutiSelect, string isEdit, string tbSourceName, string isAll, string isOwner)
            {
                this.tableName = tb.Attribute(tbname).Value;
                if (tb.Attribute(tbSourceName) != null)
                {
                    this.tableSourceName = tb.Attribute(tbSourceName).Value;
                }
                if (tb.Attribute(isExport) != null)
                {
                    this.IsExport = Convert.ToBoolean(tb.Attribute(isExport).Value);
                }
                if (tb.Attribute(isMutiSelect) != null)
                {
                    this.IsMutiSelect = Convert.ToBoolean(tb.Attribute(isMutiSelect).Value);
                }
                if (tb.Attribute(isEdit) != null)
                {
                    this.IsEdit = Convert.ToBoolean(tb.Attribute(isEdit).Value);
                }
                if (tb.Attribute(isAll) != null)
                {
                    this.IsAll = Convert.ToBoolean(tb.Attribute(isAll).Value);
                }
                if (tb.Attribute(isOwner) != null)
                {
                    this.IsOwner = Convert.ToBoolean(tb.Attribute(isOwner).Value);
                }
                var colum = from col in tb.Elements()
                            select new ReportTableColumn(col, "Name", "SourceName", "Visible", "IsData", "IsGroupBy", "IsKey", "IsFixedStyle", "IsLink", "Url", "TextField", "FieldName");
                this.columns = colum.ToList();

            }

            public bool IsExport { get; set; }
            public bool IsMutiSelect { get; set; }
            public bool IsEdit { get; set; }
            public bool IsAll { get; set; }
            public bool IsOwner { get; set; }
            public string tableName { get; set; }
            public string tableSourceName { get; set; }
            public List<ReportTableColumn> columns { get; set; }

        }

        public class ReportTableColumn
        {
            /// <summary>
            /// 初始化列
            /// </summary>
            /// <param name="columnElement"></param>
            /// <param name="lst"></param>
            public ReportTableColumn(XElement columnElement, string column, string sourcecolumn, string Visible, string data, string groupby, string iskey, string isFixedStyle, string IsLink, string Url, string TextField, string FieldName)
            {


                var attribute = columnElement.Attribute(column);
                if (attribute != null) this.columnName = attribute.Value;
                var source = columnElement.Attribute(sourcecolumn);
                if (source != null)
                    this.sourceColumnName = source.Value;
                if (columnElement.Attribute(Visible) != null)
                {
                    var xAttribute = columnElement.Attribute(Visible);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.IsVisible = Convert.ToBoolean(xAttribute.Value);
                    }
                }
                else
                {
                    this.IsVisible = true;
                }
                if (columnElement.Attribute(data) != null)
                {
                    var xAttribute = columnElement.Attribute(data);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.IsData = Convert.ToBoolean(xAttribute.Value);
                    }
                }
                else
                {
                    this.IsData = true;
                }
                if (columnElement.Attribute(groupby) != null)
                {
                    var xAttribute = columnElement.Attribute(groupby);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.IsGoupBy = Convert.ToBoolean(xAttribute.Value);
                    }
                }
                else
                {
                    this.IsGoupBy = false;
                }
                if (columnElement.Attribute(iskey) != null)
                {
                    var xAttribute = columnElement.Attribute(iskey);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.IsKey = Convert.ToBoolean(xAttribute.Value);
                    }
                }
                else
                {
                    this.IsKey = false;
                }

                if (columnElement.Attribute(isFixedStyle) != null)
                {
                    var xAttribute = columnElement.Attribute(isFixedStyle);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.IsFixedStyle = Convert.ToBoolean(xAttribute.Value);
                    }
                }
                else
                {
                    this.IsFixedStyle = false;
                }


                if (columnElement.Attribute(IsLink) != null)
                {
                    var xAttribute = columnElement.Attribute(IsLink);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.IsLink = Convert.ToBoolean(xAttribute.Value);
                    }
                }
                else
                {
                    this.IsLink = false;
                }


                if (columnElement.Attribute(Url) != null)
                {
                    var xAttribute = columnElement.Attribute(Url);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.Url = xAttribute.Value;
                    }
                }
                else
                {
                    this.Url = null;
                }

                if (columnElement.Attribute(TextField) != null)
                {

                    var xAttribute = columnElement.Attribute(TextField);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.TextField = xAttribute.Value;
                    }
                }
                else
                {
                    this.TextField = null;
                }

                if (columnElement.Attribute(FieldName) != null)
                {

                    var xAttribute = columnElement.Attribute(FieldName);
                    if (xAttribute != null && !string.IsNullOrEmpty(xAttribute.Value))
                    {
                        this.FieldName = xAttribute.Value;
                    }
                }
                else
                {
                    this.FieldName = null;
                }


            }

            /// <summary>
            /// 字段
            /// </summary>
            public string columnName { get; set; }
            /// <summary>
            /// 表头
            /// </summary>
            public string sourceColumnName { get; set; }
            //是否可视
            public bool IsVisible { get; set; }
            //是否有数据源绑定
            public bool IsData { get; set; }
            //是否靠此字段分组
            public bool IsGoupBy { get; set; }
            //是否主见
            public bool IsKey { get; set; }
            /// <summary>
            /// 是否固定
            /// </summary>
            public bool IsFixedStyle { get; set; }
            //此列是否是连接
            public bool IsLink { get; set; }
            //连接地址
            public string Url { get; set; }

            //链接显示字段
            public string TextField { get; set; }

            //传过去的字段名称
            public string FieldName { get; set; }

            ///// <summary>
            ///// 是否多选
            ///// </summary>
            //public bool IsMultiSelect { get; set; }


            //    if (lst.Count > 0)
            //    {
            //        foreach (var co in lst)
            //        {
            //            var attribute = columnElement.Attribute(co);
            //            if (attribute != null)
            //            {
            //                switch (co)
            //                {
            //                    case "Name":
            //                        this.columnName = attribute.Value;
            //                        break;
            //                    case "SourceName":
            //                        this.sourceColumnName = attribute.Value;
            //                        break;
            //                    case "Visible":
            //                        this.isVisible = Convert.ToBoolean(attribute.Value);
            //                        break;
            //                    case "IsData":
            //                        this.isData = Convert.ToBoolean(attribute.Value);
            //                        break;
            //                    case "IsGroupBy":
            //                        this.isGroupBy = Convert.ToBoolean(attribute.Value);
            //                        break;
            //                    case "IsKey":
            //                        this.isKey = Convert.ToBoolean(attribute.Value);
            //                        break;
            //                    case "IsFixedStyle":
            //                        this.isFixedStyle = Convert.ToBoolean(attribute.Value);
            //                        break;
            //                    case "IsMultiSelect":
            //                        this.isMultiSelect = Convert.ToBoolean(attribute.Value);
            //                        break;

            //                }
            //            }

            //        }
            //    }




            //}


            ///// <summary>
            ///// 字段
            ///// </summary>
            //public string columnName { get; set; }
            ///// <summary>
            ///// 表头
            ///// </summary>
            //public string sourceColumnName { get; set; }

            //private bool isVisible = false;

            ////是否可视
            //public bool IsVisible
            //{
            //    get { return isVisible; }
            //    set
            //    {
            //        if (value == null)
            //        {
            //            isVisible = false;
            //        }
            //    }
            //}
            //private bool isData = true;

            ////是否有数据源绑定
            //public bool IsData
            //{
            //    get { return isData; }
            //    set
            //    {
            //        if (value == null)
            //        {
            //            isData = false;
            //        }
            //    }
            //}
            //private bool isGroupBy = false;

            ////是否靠此字段分组
            //public bool IsGoupBy
            //{
            //    get { return isGroupBy; }
            //    set
            //    {
            //        if (value == null)
            //        {
            //            isGroupBy = false;
            //        }
            //    }
            //}
            //private bool isKey = false;

            ////是否主键
            //public bool IsKey
            //{
            //    get { return isKey; }
            //    set
            //    {
            //        if (value == null)
            //        {
            //            isKey = false;
            //        }
            //    }
            //}

            //private bool isFixedStyle = false;

            ////是否固定
            //public bool IsFixedStyle
            //{
            //    get { return isFixedStyle; }
            //    set
            //    {
            //        if (value == null)
            //        {
            //            isFixedStyle = false;
            //        }
            //    }
            //}

            //private bool isMultiSelect = false;


            ///// <summary>
            ///// 是否多选
            ///// </summary>
            //public bool IsMultiSelect
            //{
            //    get { return isMultiSelect; }
            //    set
            //    {
            //        if (value == null)
            //        {
            //            isMultiSelect = false;
            //        }
            //    }
            //}

        }
    }
}
